# @Time    : 2020/2/23 18:15
# @Author  : wumao
# @Email   : kanhebei@dingtalk.com

import os


def register_configuration(app):
    # 自动加载config目录下的 .py 文件载入配置项
    _dir = os.listdir(os.path.join(app.root_path, 'config'))
    for py in _dir:
        if not py.startswith('_'):
            app.config.from_pyfile(os.path.join(os.path.dirname(__file__), py))

    # 加载public/site.py
    site = os.path.join(app.root_path, '..', 'public', 'site.py')
    if os.path.exists(site):
        app.config.from_pyfile(site)

    env = os.getenv('FLASK_ENV', 'production')

    if env == 'development':
        app.config.update(
            DEBUG=True,
            DEBUG_TB_INTERCEPT_REDIRECTS = False,
            DEBUG_TB_ENABLED = True
        )
    elif env == 'testing':
        app.config.update(
            DEBUG=True,
            DEBUG_TB_INTERCEPT_REDIRECTS = True,
            DEBUG_TB_ENABLED = False,
        )
    else:
        app.config.update(
            DEBUG=False,
            DEBUG_TB_INTERCEPT_REDIRECTS=False,
            DEBUG_TB_ENABLED=False
        )
